package net.xxcl.exam.service;

import java.util.List;

import net.xxcl.exam.mapper.RoleMapper;
import net.xxcl.exam.mapper.UserMapper;
import net.xxcl.exam.mapper.UserRoleMapper;
import net.xxcl.exam.pojo.Criteria;
import net.xxcl.exam.pojo.Role;
import net.xxcl.exam.pojo.User;
import net.xxcl.exam.pojo.UserRoleKey;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional()
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    private static final Logger logger = LoggerFactory.getLogger(UserService.class);

    @Transactional(readOnly = true)
    public int countByExample(Criteria example) {
        int count = this.userMapper.countByExample(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Transactional(readOnly = true)
    public User selectByPrimaryKey(Integer id) {
        return this.userMapper.selectByPrimaryKey(id);
    }

    @Transactional(readOnly = true)
    public List<User> selectByExample(Criteria example) {
        return this.userMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(Integer id) {
        return this.userMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(User record) {
        return this.userMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(User record) {
        return this.userMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(Criteria example) {
        return this.userMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(User record, Criteria example) {
        return this.userMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(User record, Criteria example) {
        return this.userMapper.updateByExample(record, example);
    }

    public int insert(User record) {
        return this.userMapper.insert(record);
    }

    public int insertSelective(User record) {
        return this.userMapper.insertSelective(record);
    }

    /**
     * 登录验证
     * @param name
     * @param password
     * @return
     */
    @Transactional(readOnly = true)
    public User login(String name,String password){
        Criteria criteria = new Criteria();
        criteria.put("name",name);
        criteria.put("password",password);
        List<User> list = userMapper.selectByExample(criteria);
        if (list.size()>0){
            return list.get(0);
        } else {
            return null;
        }
    }

    /**
     * 获取用户角色id
     * @return
     */
    @Transactional(readOnly = true)
    public List<Integer> getRoleIdsByUserId(Integer userId){
        return userMapper.getRoleIdsByUserId(userId);
    }

    /**
     * 获取用户角色列表
     * @return
     */
    @Transactional(readOnly = true)
    public List<Role> getRolesByUserId(Integer userId){
        return roleMapper.getRolesByUserId(userId);
    }

    /**
     * 删除用户
     * @param ids
     * @return
     */
    public int deleteUser(String ids) {
        String[] str = ids.split(",");
        for(String id:str){
            this.userMapper.deleteByPrimaryKey(Integer.parseInt(id));
        }
        return str.length;
    }

    /**
     * 重置密码
     * @param ids
     * @return
     */
    public int resetPass(String ids,String password) {
        String[] str = ids.split(",");
        for(String id:str){
            User user = new User();
            user.setId(Integer.parseInt(id));
            user.setPassword(password);
            this.userMapper.updateByPrimaryKeySelective(user);
        }
        return str.length;
    }

    /**
     * 关联角色
     * @param roleids
     * @param userids
     * @return
     */
    public int glRole(String roleids,String userids){
        String[] useridsStr = userids.split(",");
        String[] roleidsStr = roleids.split(",");
        UserRoleKey userRoleKey = new UserRoleKey();
        for(String useridStr:useridsStr){
            int userid = Integer.parseInt(useridStr);
            //把之前的删除
            userRoleMapper.deleteByUserId(userid);
            for(String roleidStr:roleidsStr){
                int roleid = Integer.parseInt(roleidStr);
                userRoleKey.setUserid(userid);
                userRoleKey.setRoleid(roleid);
                userRoleMapper.insert(userRoleKey);
            }
        }
        return useridsStr.length;
    }
}